package com.terrace.web.tagext.easyui.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * JS函数
 * @author jiangyg
 *
 */
public class JSFunction implements Serializable {

	private static final long serialVersionUID = 7050401975790170443L;

	/**
	 * 函数名称
	 */
	private String funcName;
	
	/**
	 * 函数体
	 */
	private StringBuffer funcBody = new StringBuffer();
	
	/**
	 * 函数参数
	 */
	private List<String> funcParams = new ArrayList<String>();
	
	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}

	public void addFuncParams(String... funcParams) {
		if (funcParams != null) {
			for (String param : funcParams) {
				this.funcParams.add(param);
			}
		}
	}
	
	public void addFuncBody(String body) {
		funcBody.append(body);
	}

	/**
	 * 转化为JS函数
	 * @return
	 */
	public String transferJSFunction() {
		StringBuffer sb = new StringBuffer();
		sb.append("function");
		if (StringUtils.isNotBlank(funcName)) {
			sb.append(" " + funcName);
		}
		sb.append("(");
		for (int i = 0; i < funcParams.size(); i++) {
			sb.append(funcParams.get(i));
			if (i < funcParams.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("){" + funcBody.toString() + "}");
		return sb.toString();
	}
	
	/**
	public static void main(String[] args) {
		JSFunction jsf = new JSFunction();
		jsf.setFuncParams("a", "b", "c");
		jsf.setFuncBody("var q = 0;");
//		jsf.setFuncName("gujt");
		System.out.println(jsf.transferJSFunction());
	}
	**/

}
